package shplate

class BaseController {

    def dynamicQueryService
    def exportFileService

    def exportExcel() {
        def sysQueryName
        def tab = params.timeType
        if (tab.equals('H')) {
            sysQueryName = params.SYS_QUERY_NAME + 'HList'
        } else if (tab.equals('D')) {
            sysQueryName = params.SYS_QUERY_NAME + 'DList'
        } else if (tab.equals('W')) {
            sysQueryName = params.SYS_QUERY_NAME + 'WList'
        } else if (tab.equals('M')) {
            sysQueryName = params.SYS_QUERY_NAME + 'MList'
        } else if (tab.equals('Q')) {
            sysQueryName = params.SYS_QUERY_NAME + 'QList'
        } else if (tab.equals('Y')) {
            sysQueryName = params.SYS_QUERY_NAME + 'YList'
        } else {
            sysQueryName = params.SYS_QUERY_NAME
        }
        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList])
    }

    def expExcel() {
        def sysQueryName
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = params.startTime != null ? URLDecoder.decode(params.startTime, 'UTF-8') : params.startTime
        params.endTime = params.endTime != null ? URLDecoder.decode(params.endTime, 'UTF-8') : params.endTime
        params.state = params.state ? Arrays.asList(params.state) : (params['state[]'] ? Arrays.asList(params['state[]']) : '')
        params.gzzx = params.gzzx ? Arrays.asList(params.gzzx) : (params['gzzx[]'] ? Arrays.asList(params['gzzx[]']) : '')
        params.transType = params.transType ? Arrays.asList(params.transType) : (params['transType[]'] ? Arrays.asList(params['transType[]']) : '')
        sysQueryName = params.SYS_QUERY_NAME
        params.SYS_QUERY_NAME = sysQueryName
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        def result = g.render(template: "exp${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
        exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", result)
    }
}